
library(plyr)
library(xtable)

####
##Code Up Study 1 Demographics
####

d<-read.csv("Limits_of_Partisan_Group_Membership.csv")

d <- d[d$Q1.12 !="",]
study1 <- d
study1 <- study1[,c('Q1.3','Q1.4','Q1.5','Q1.6','Q1.7','Q1.8','Q1.12')]
study1 <- subset(study1,study1$Q1.12 %in% c('Democrat','Republican'))

#Gender
study1$male <- ifelse(study1$Q1.3=='Male', 1, 0)
study1$female <- ifelse(study1$Q1.3=='Female', 1, 0)

#Race
study1$Q1.5 <- as.character(study1$Q1.5)
study1$Q1.5[which(study1$Q1.6=='Yes')] <- 'Hispanic'

study1$black <- ifelse(study1$Q1.5=='Black',1,0)
study1$white <-  ifelse(study1$Q1.5=='White',1,0)
study1$hispanic <-  ifelse(study1$Q1.5=='Hispanic',1,0)
study1$other.race <- ifelse( (study1$black==0 & study1$white==0 & study1$hispanic==0), 1, 0)

#Education
study1$college <- ifelse(study1$Q1.7 %in% c("4-year College Degree","Doctoral Degree","Professional Degree (JD, MD)","Masters Degree"),1,0)
study1$not.college <- ifelse(study1$Q1.7 %in% c("4-year College Degree","Doctoral Degree","Professional Degree (JD, MD)","Masters Degree"),0,1)

#Income
study1$Q1.4 <- as.character(study1$Q1.4)
study1$income <- as.numeric(as.character(mapvalues(x=study1$Q1.4,from=c("Less than 30,000","30,000 \x89\xdb\xd2 39,999","40,000 \x89\xdb\xd2 49,999","50,000 \x89\xdb\xd2 59,999","60,000 \x89\xdb\xd2 69,999","70,000 \x89\xdb\xd2 79,999","80,000 \x89\xdb\xd2 89,999","90,000 \x89\xdb\xd2 99,999","100,000 or more"),to=c(30000,35000,45000,55000,65000,75000,85000,95000,100000))))

#Partisanship
study1$democrat <- ifelse(study1$Q1.12=='Democrat', 1, 0)
study1$republican <- ifelse(study1$Q1.12=='Republican', 1, 0)

#Age
study1$age <- as.numeric(as.character(mapvalues(x=study1$Q1.8,from=c('18-25','26-34','35-54','55-64','65 or over'),to=c(21.5,30,44.5,59.5,65))))

####
##Code Up Study 2 Demographics
####
study2 <- read.csv('CombinedSurvey2.csv')
study2 <- subset(study2,study2$gc==1)
study2 <- subset(study2,study2$divisive.rounds.round1porder!='')

study2$black <- ifelse(study2$Q3=='African American',1,0)
study2$white <- ifelse(study2$Q3=='White/Caucasian',1,0)
study2$hispanic <- ifelse(study2$Q3=='Hispanic',1,0)
study2$other.race <- ifelse(study2$Q3 %in% c('Pacific Islander','Asian','Native American','Other'),1,0)
study2$non.white <- ifelse(study2$Q3=='White/Caucasian',0,1)

study2$male <- ifelse(study2$Q2=='Male',1,0)
study2$female <- ifelse(study2$male==1,0,1)

study2$democrat <- ifelse(study2$Q4=='Democrat',1,0)
study2$republican <- ifelse(study2$Q4=='Republican',1,0)
study2$independent <- ifelse(study2$Q4 %in% c('Democrat','Republican'), 0, 1)
study2 <- subset(study2,study2$independent==0)

study2$age <- as.numeric(as.character(study2$Q5))
study2$income <- as.numeric(as.character(mapvalues(x=study2$Q6,from=c("Less than 30,000","30,000 – 39,999","40,000 – 49,999","50,000 – 59,999","60,000 – 69,999","70,000 – 79,999","80,000 – 89,999","90,000 – 99,999","100,000 or more"),to=c(30000,35000,45000,55000,65000,75000,85000,95000,100000))))

study2$college <- as.numeric(as.character(mapvalues(x=study2$Q7,from=c("Less than High School","High School / GED","Some College","2-year College Degree","4-year College Degree","Masters Degree","Professional Degree (JD, MD)","Doctoral Degree"),to=c(0,0,0,0,1,1,1,1))))
study2$not.college <- ifelse(study2$college==0,1,0)

#Subset to Good Completes
####
##Combined into table
####

study1.demos <- study1[,c('black','white','hispanic','other.race','college','not.college','female','male','age','income','democrat','republican')]
study1.demos$study <- 'study1'

study2.demos <- study2[,c('black','white','hispanic','other.race','college','not.college','female','male','age','income','democrat','republican')]
study2.demos$study <- 'study2'

combined.demos <- rbind(study1.demos,study2.demos)

demo.frame <- ddply(combined.demos,.(study),summarise,black=mean(black,na.rm=TRUE),hispanic=mean(hispanic,na.rm=TRUE),white=mean(white,na.rm=TRUE),other.race=mean(other.race,na.rm=TRUE),college=mean(college,na.rm=TRUE),no.college=mean(not.college,na.rm=TRUE),female=mean(female,na.rm=TRUE),male=mean(male,na.rm=TRUE),age=mean(age,na.rm=TRUE),income=mean(income,na.rm=TRUE),democrat=mean(democrat,na.rm=TRUE),republican=mean(republican,na.rm=TRUE),sample.size=length(study))
demo.frame[,c(2:13)] <- round(demo.frame[,c(2:13)],digits=2)
demo.frame <- as.data.frame(t(demo.frame))
xtable(demo.frame)